/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxConfPoster;
import com.ykht.proj.coupon.service.FxConfPosterService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 海报配置表
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxconfposter")
@Api(value = "fxconfposter", tags = "海报配置表管理")
public class FxConfPosterController {

	private final FxConfPosterService fxConfPosterService;

	/**
	 * 查询门店海报底底板
	 */
	@ApiOperation(value = "查询全部", notes = "查询全部")
	@GetMapping("/getBookPoster")
	public R getBookPoster() {
		return R.ok(fxConfPosterService.list(Wrappers.lambdaQuery(FxConfPoster.class)
				.eq(FxConfPoster::getDelFlag, 0)
				.eq(FxConfPoster::getType, 2)
		));
	}

	/**
	 * 查询全部
	 */
	@ApiOperation(value = "查询全部", notes = "查询全部")
	@GetMapping("/findAll")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfposter_view')")
	public R getFxConfPoster() {
		Map<String, Object> map = new HashMap<>();
		map.put("del_flag", 0);
		List<FxConfPoster> list = fxConfPosterService.listByMap(map);
		//分组
		Map<Integer, List<FxConfPoster>> listMap = list.stream().collect(Collectors.groupingBy(FxConfPoster::getType));
		return R.ok(listMap);
	}


	/**
	 * 通过id查询海报配置表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfposter_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxConfPosterService.getById(id));
	}

	/**
	 * 新增或修改海报配置表
	 *
	 * @param list 海报配置表
	 * @return R
	 */
	@ApiOperation(value = "新增或修改海报配置表", notes = "新增或修改海报配置表")
	@SysLog("新增或修改海报配置表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxconfposter_add')")
	public R save(@RequestBody List<FxConfPoster> list) {
		if (null == list || list.size() < 0) {
			return R.failed("至少保留一个海报");
		}
		fxConfPosterService.remove(Wrappers.lambdaQuery(FxConfPoster.class).eq(FxConfPoster::getType, list.get(0).getType()));
		//添加
		list.stream().map(o -> {
			o.setCreateTime(LocalDateTime.now());
			o.setCreateUser(SecurityUtils.getUserId());
			return o;
		});
		return R.ok(fxConfPosterService.saveOrUpdateBatch(list));
	}


}
